﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HRMS_Model;
using DataManager;
using System.Diagnostics;

namespace HRMS.Models
{
    public class RoleModels
    {
        public static bool AddRole(Role role)
        {
            try
            {
                using (var roleEntity = new HRMSEntities())
                {
                    roleEntity.Roles.AddObject(role);
                    roleEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public static List<Role> GetRoles(string sortExpression, string sortDirection, int pageIndex, int pageSize, int pageCount)
        {
            using (var roleEmtity = new HRMSEntities())
            {
                return roleEmtity.Roles.OrderBy("it." + sortExpression + " " + sortDirection).Skip(pageIndex * pageSize).Take(pageCount * pageSize).ToList();
            }
        }

        public static int GetRolesCount()
        {
            using (var roleEntity = new HRMSEntities())
            {
                return roleEntity.Roles.Count();
            }
        }

        public static bool UpdateRole(Role role)
        {
            try
            {
                using (var roleEntity = new HRMSEntities())
                {
                    var roleToEdit = roleEntity.Roles.FirstOrDefault(r => r.RoleId == role.RoleId);
                    if (roleToEdit != null) roleToEdit.RoleType = role.RoleType;
                    roleEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public static bool DeleteRole(int id)
        {
            try
            {
                using (var roleEntity = new HRMSEntities())
                {
                    roleEntity.Roles.DeleteObject(roleEntity.Roles.First(r => r.RoleId == id));
                    roleEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public static List<string> RoleNames()
        {
            var roleNameList = new List<string>();
            try
            {
                using (var roleEntity = new HRMSEntities())
                {
                    var rolesList = roleEntity.Roles.ToList();
                    roleNameList.AddRange(rolesList.Select(role => role.RoleType));
                }
                return roleNameList;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return roleNameList;
            }
        }
    }
}